package com.power.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.power.domain.ProdProp;
import com.power.domain.ProdPropValue;
import com.power.model.Result;
import com.power.service.ProdPropService;
import com.power.service.ProdPropValueService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Description 商品规格管理控制层
 * @Author dingchunlin
 * @Date 2024/07/14 23:02
 * @Version 1.0.0
 */
@Api(tags = "商品规格接口管理")
@RestController
@RequestMapping("/prod/spec")
public class ProdSpecController {

    @Autowired
    private ProdPropService prodSpecService;

    @Autowired
    ProdPropValueService prodPropValueService;

    /**
     * 多条件分页查询商品规格列表
     * @param current 当前页
     * @param size 每页显示数量
     * @param propName 规格名称
     * @return Result<Page<ProdProp>>
     */
    @ApiOperation(value = "多条件分页查询商品规格列表")
    @RequestMapping("/page")
    @PreAuthorize("hasAuthority('prod:spec:page')")
    public Result<Page<ProdProp>> loadProdSpecPage(@RequestParam Long current,
                                                   @RequestParam Long size,
                                                   @RequestParam(required = false) String propName){
        // 调用service层方法查询商品规格列表
        Page<ProdProp> page = prodSpecService.queryProdSpecPage(current, size, propName);
        return Result.success(page);
    }

    /**
     * 新增商品规格
     * @param prodProp 商品规格实体类
     * @return Result<String>
     */
    @ApiOperation(value = "新增商品规格")
    @PostMapping
    @PreAuthorize("hasAuthority('prod:spec:save')")
    public Result<String> saveProdSpec(@RequestBody ProdProp prodProp) {
        // 调用service层方法新增商品规格
        boolean save = prodSpecService.saveProdSpec(prodProp);
        return Result.handle(save);
    }

    /**
     * 修改商品规格
     * @param prodProp 商品规格实体类
     * @return Result<String>
     */
    @ApiOperation(value = "修改商品规格")
    @PutMapping
    @PreAuthorize("hasAuthority('prod:spec:update')")
    public Result<String> modifyProdSpec(@RequestBody ProdProp prodProp) {
        // 调用service层方法修改商品规格
        Boolean modified = prodSpecService.modifyProdSpec(prodProp);
        return Result.handle(modified);
    }

    /**
     * 删除商品规格
     * @param id 商品规格id
     * @return Result<String>
     */
    @ApiOperation(value = "删除商品规格")
    @DeleteMapping("/{id}")
    @PreAuthorize("hasAuthority('prod:spec:delete')")
    public Result<String> removeProdSpec(@PathVariable Long id) {
        // 调用service层方法删除商品规格
        Boolean remove = prodSpecService.removeProdSpecByProdId(id);
        return Result.handle(remove);
    }

    /**
     * 查询系统商品属性集合
     * @return Result<List<ProdProp>>
     */
    @ApiOperation(value = "查询系统商品属性集合")
    @GetMapping("/list")
    @PreAuthorize("hasAuthority('prod:spec:page')")
    public Result<List<ProdProp>> loadProdPropList () {
        List<ProdProp> prodPropList = prodSpecService.queryProdPropList();
        return Result.success(prodPropList);
    }

    /**
     * 根据商品id查询规格值集合
     * @param prodId 商品id
     * @return Result<List<ProdPropValue>>
     */
    @ApiOperation(value = "根据商品id查询规格值集合")
    @GetMapping("/listSpecValue/{prodId}")
    @PreAuthorize("hasAuthority('prod:spec:page')")
    public Result<List<ProdPropValue>> loadProdPropValue (@PathVariable Long prodId) {
        List<ProdPropValue> prodPropValueList = prodPropValueService.queryProdPropValuesByProdId(prodId);
        return Result.success(prodPropValueList);
    }
}
